<template>
  <div>
    <!--头部行表单 ：搜索 导出 -->
    <div class="filter-container">
      <el-input v-model="query.keyword" placeholder="搜索用户" style="width: 200px;" class="filter-item" @keyup.enter.native="handleSearch" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
    </div>

    <!-- 数据列表  删除 -->
    <div class="list-container">
      <el-table :data="dataList" fit highlight-current-row style="width: 100%;">
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="收货人"></el-table-column>
        <el-table-column prop="status" label="状态"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>

        <el-table-column label="操作" width="80" align="center">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="recordTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import * as OrderAPI from '@/api/order.js';
export default {
  name: 'order',
  data() {
    return {
      dataList: [], //列表数据
      query: {
        keyword: '',
        pageIndex: 1,
        pageSize: 10,
        orderBy: 'id'
      },
      recordTotal: 0,
      exportLoading: false //导出 还没有实现 ，需要继续
    };
  },

  created() {
    // 获取数据
    this.loadDataList();
  },

  methods: {
    // 获取列表数据
    loadDataList() {
      OrderAPI.getOrderPage(this.query).then(result => {
        this.dataList = result.data;
        this.recordTotal = result.recordTotal;
      });
    },

    // 搜索按钮
    handleSearch() {
      this.query.pageIndex = 1;
      this.loadDataList();
    },

    // 触发导出按钮
    handleExport() {
      this.$message('导出，待完成...');
    },

    // 删除操作
    handleDelete(index) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 选择了确认，则删除后端数据库中的记录
          let id = this.dataList[index].id; //要删除记录的id
          OrderAPI.deleteOrderById(id)
            .then(() => {
              this.dataList.splice(index, 1); //同步列表中的记录
              this.$message.success('删除成功');
            })
            .catch(() => {});
        })
        .catch(() => {});
    },

    // 分页导航=======================================
    handlePageChange(val) {
      this.query.pageIndex = val;
      this.loadDataList();
    }
  }
};
</script>

<style scoped></style>
